package com.dreamlin.basepro.base.view.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.MenuRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.dreamlin.basepro.base.view.LoadingDialog;
import com.dreamlin.basepro.common.FastClickUtils;
import com.gyf.immersionbar.ImmersionBar;

public abstract class WithoutVMFragment<T extends ViewBinding> extends Fragment implements View.OnClickListener {

    protected T binding;

    protected LoadingDialog loadingDialog;

    protected Toast toast;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initBars();
        binding = getBinding(inflater, container);
        initView();
        initData();
        return binding.getRoot();
    }

    @NonNull
    protected abstract T getBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container);

    @MenuRes
    protected int getMenu() {
        return -0x01;
    }

    protected abstract void initView();

    protected abstract void initData();

    protected void initBars() {
        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    public void onClick(View v) {
        if (!FastClickUtils.isFastDoubleClick(v.getId())) {
            click(v);
        }
    }

    protected abstract void click(View view);

    protected void showLoading() {
        showLoading(null);
    }

    protected void showLoading(@Nullable String tips) {
        if (loadingDialog != null) {
            loadingDialog.setMsg(tips);
        } else {
            loadingDialog = new LoadingDialog(getActivity());
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    protected void hideLoading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.hide();
        }
    }

    @SuppressLint("ShowToast")
    protected void toast(@StringRes int strId) {
        if (toast == null) {
            toast = Toast.makeText(getContext(), strId, Toast.LENGTH_SHORT);
        } else {
            toast.setText(strId);
        }
        toast.show();
    }

    @SuppressLint("ShowToast")
    protected void toast(String str) {
        if (toast == null) {
            toast = Toast.makeText(getContext(), str, Toast.LENGTH_SHORT);
        } else {
            toast.setText(str);
        }
        toast.show();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            getActivity().onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
